package com.cms.controller.configuration;

import com.cms.common.JsonResult;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.mock.base.util.CollectionUtil;
import com.mock.base.util.NumberUtil;
import com.mock.base.util.StringUtil;

import java.util.*;

public class ConfigurationController extends Controller {

    public void sysAndMolAdd(){

        //查询全部系统名称到页面
        List<Record> systemList = Db.find("SELECT * FROM system_manage WHERE status = 1");
        setAttr("systemList",systemList);
        renderJsp("/pages/configuration/system_module_add.jsp");
    }

    public void sysAndMolAddSave(){
        String systemId = getPara("systemId");
        String module_name = getPara("module_name");

        //先查找添加的系统是否在表中已经存在
        Record systemExit = Db.findFirst("SELECT * FROM system_manage WHERE id = ?", systemId);

        if(systemExit!=null){
            Record module = new Record();
                module.set("system_id",systemId);
                module.set("module_name",module_name);
                module.set("create_time",new Date());
                module.set("update_time",new Date());
                Db.save("module_manage",module);
        }

        renderJsp("/pages/configuration/system_module_add.jsp");
    }

    public void newSysAndMolAddSave(){
        String systemName = getPara("systemName");
        String moduleName = getPara("moduleName");

        //先查找添加的系统是否在表中已经存在
        Record systemExit = Db.findFirst("SELECT * FROM system_manage WHERE system_name = ?", systemName);

        //如果不存在,系统和模块都需要新增,如果已经存在系统名,系统表不需要添加,模块表新增一条
        if(systemExit==null){

            Record system= new Record();
            Record module = new Record();

            if(systemName!=null){
                //保存系统信息
                system.set("system_name",systemName);
                system.set("create_time",new Date());
                system.set("update_time",new Date());
                Db.save("system_manage",system);
            }

            if(moduleName!=null){
                //保存模块信息
                module.set("system_id",system.getLong("id"));
                module.set("module_name",moduleName);
                module.set("create_time",new Date());
                module.set("update_time",new Date());
                Db.save("module_manage",module);
            }
        }else {
            Record module = new Record();
            if(moduleName!=null){
                //保存模块信息
                module.set("system_id",systemExit.getLong("id"));
                module.set("module_name",moduleName);
                module.set("create_time",new Date());
                module.set("update_time",new Date());
                Db.save("module_manage",module);
            }
        }

        renderJsp("/pages/configuration/system_module_add.jsp");
    }

    public void pagination(){

        int pageNumber = getParaToInt("pageNumber");
        int pageSize = getParaToInt("pageSize");

        Page<Record> recordList = Db.paginate(pageNumber, pageSize, "SELECT s.*,m.* ", " FROM system_manage s JOIN module_manage m ON s.id = m.system_id ORDER BY s.id");

        Map<String, Object> returnMap = new HashMap<String, Object>();
        returnMap.put("total", recordList.getTotalRow());
        returnMap.put("rows", recordList.getList());
        renderJson(returnMap);
    }

    @Before(Tx.class)
    public void delete() {

        String module_id = getPara("system_module_id");
        Record module = Db.findById("module_manage", module_id);
        Long module_count = Db.queryLong("SELECT COUNT(id) FROM module_manage WHERE system_id IN (?)", module.getLong("system_id"));

        if(module_count==1){
            //系统和模块数据以前删除
            Db.update("delete from system_manage where id = ?",module.getLong("system_id"));
            Db.update("delete from module_manage where id = ?",module_id);
        }else {
            //说明只需要删除模块表数据,系统数据不需要删除
            Db.update("delete from module_manage where id = ?",module_id);
            Record system = Db.findById("system_manage", module.getLong("system_id"));
            system.set("update_time",new Date());
            Db.update("system_manage",system);
        }

        renderJson(new JsonResult().status("000").show("删除成功"));
    }

    public void edit(){

        String module_id = getPara("id");
        Record record = Db.findFirst("SELECT s.*,m.* FROM system_manage s JOIN module_manage m ON s.id = m.system_id WHERE m.id = ?", module_id);
        if(record != null){
            setAttr("system_module",record);
        }

        renderJsp("/pages/configuration/add.jsp");
    }

    //修改系统名称
    public void systemNameEdit(){

        String systemId = getPara("systemId");
        Record record = Db.findFirst("SELECT * FROM system_manage WHERE id = ?", systemId);
        if(record != null){
            setAttr("system",record);
        }

        renderJsp("/pages/configuration/systemNameEdit.jsp");
    }

    @Before(Tx.class)
    public void save(){

        //模块id
        String module_id = getPara("id");
        String system_name = getPara("system_name");
        String module_name = getPara("module_name");

        Record module = new Record();
        if(StringUtil.isNotEmpty(module_id)){
            module = Db.findById("module_manage",module_id);
        }
        module.set("module_name",module_name);
        if(StringUtil.isNotEmpty(module_id)){
            Db.update("module_manage",module);
        }

        //修改接口表的模块名称
        List<Record> interfaceList = Db.find("SELECT * FROM cms_interface WHERE module_id = ?", module_id);
        interfaceList.forEach(item->{
            item.set("module_name",module_name);
            Db.update("cms_interface",item);
        });

        renderJson(new JsonResult().status("000").show("保存成功"));
    }

    //修改系统名称保存
    @Before(Tx.class)
    public void systemNameEditToSave(){

        //模块id
        String system_id = getPara("id");
        String system_name = getPara("systemName");
        String status = getPara("status");

        //修改名字
        Record system = new Record();
        if(StringUtil.isNotEmpty(system_id)){
            system = Db.findById("system_manage",system_id);
            system.set("system_name",system_name);
            //修改名字和状态
            if(status.equals(0)){
                system.set("status",status);
            }else if (status.equals(1)){
                system.set("status",status);
            }
            Db.update("system_manage",system);
        }

        //修改接口表的系统名称
        List<Record> interfaceList = Db.find("select * from cms_interface where system_id = ?", system_id);
        if(CollectionUtil.isNotEmpty(interfaceList)){
            for (Record item:interfaceList){
                item.set("system_name",system_name);
                Db.update("cms_interface",item);
            }
        }

        renderJson(new JsonResult().status("000").show("保存成功"));
    }

    @Before(Tx.class)
    public void systemModuleStop(){
        String module_id = getPara("module_id");
        Record module = Db.findById("module_manage", module_id);

        if(module!=null){
            module.set("status",0).set("update_time",new Date());
            Db.update("module_manage",module);
        }
        renderJson(new JsonResult().status("000").show("停止成功"));
    }

    @Before(Tx.class)
    public void systemModuleStart(){
        String module_id = getPara("module_id");
        Record module = Db.findById("module_manage", module_id);

        if(module!=null&&module.getInt("status")==0){
            module.set("status",1).set("update_time",new Date());
            Db.update("module_manage",module);
        }
        renderJson(new JsonResult().status("000").show("启动成功"));
    }
}
